import {
  SET_USERINFO,
  GET_SYSTEMINFO_SUCCESS,
  SET_SIGNLIST,
  SET_ORDERLIST,
  GET_ORDERLIST,
  SET_ORDERDETAILS,
  GET_ORDERDETAILS,
  GET_NETWORK,
  SET_AUTHORIZE,
  SET_OPENID,
  SET_SESSIONKEY
} from './mutation-type';
export default {
    getSystemInfo({commit, state}){
        return new Promise((resolve, reject) => {
            if (state.systemInfo) {
                resolve(state.systemInfo)
            } else {
                wx.getSystemInfo({
                    success(res) {
                        commit(GET_SYSTEMINFO_SUCCESS, res)
                        resolve(res)
                    },
                    fail(err){
                        reject(err)
                    }
                });
            }
            
        })
    },
    getNetwork({commit, state}){
        return new Promise((resolve, reject) => {
            wx.onNetworkStatusChange(function (res) {
              state.isConnected = res.isConnected;
              state.networkType = res.networkType;
            })
        })
        
    },
    setUserInfo({commit},data){
        commit(SET_USERINFO, data)
    },
    setOpenid({commit},data){
        commit(SET_OPENID,data)
    },
    setAuthorize({commit},data){
        commit(SET_AUTHORIZE, data)
    },
    setSessionKey({commit},data){
        commit(SET_SESSIONKEY, data)
    },
    setSignList({commit},data){
        commit(SET_USERINFO, data)
    },
    setOrderList({commit},data){
        commit(SET_ORDERLIST, data)
    },
    getOrderList({commit,state},type){
        if (type == 0) {
          return state.unsentOrders;
        } else if (type == 1) {
          return state.unacceptedOrders;
        } else if (type == 2) {
          return state.acceptedOrders;
        } else if (type == 3) {
          return state.finishedOrders;
        }
    },
    setOrderDetail({commit},order){
        commit(SET_ORDERDETAILS,order)
    },
    getOrderDetail({commit,state},id){
        return state.orderDetails[id]
    },
    setImgHost({commit,state},host){
        return state.imgHost = host
    }
}